<!--
* description: 车辆管理-验收入库
* author: ligy
* createdDate: 2020-11-16
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      ref="multipleTable"
      :dynamic-buttons="tableButtons"
      :dynamic-components="tableComponents"
      :dynamic-api-config="apiConfig"
      :dynamic-form-fields="formField"
      :dynamic-table-cols="tableCols"
      :dynamic-is-show-select="false"
      :dynamic-is-init-table="true"
      :dynamic-is-column-drop="false"
      :is-set-default-brand="false"
      :dynamic-is-fix-choose="false"
      :dynamic-is-fix-index="false"
      :dynamic-api-style="'restful'"
      :callback-obj="callbackObj"
    />
    <Edit
      :key="editPopupsKey"
      :popups-visible="editPopupsVisible"
      :title-name="titleName"
      :data-for-form="editRowData"
      @close="close"
      @save="send"
    />
  </div>
</template>

<script>
import { oneTableWithViewTemplateMixins } from '@/components/mixins/oneTableWithViewTemplateMixins'
import OneTableTemplate from '@/components/templates/oneTable'
import Edit from './edit'
import api from '@/api/vehicleManagement'
import lov from '@/api/Lov'
export default {
  components: {
    OneTableTemplate,
    Edit
  },
  mixins: [oneTableWithViewTemplateMixins],
  data() {
    return {
      titleName: '',
      apiConfig: api.getacceptanceList,
      // 动态生成网格列
      tableCols:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          .length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path].tableCols
          : [
            {
              prop: 'controlBtn',
              label: '操作',
              width: 150,
              align: 'center',
              isComponent: true,
              comps: [
                {
                  compKey: 'propKey1',
                  labelName: '验收入库',
                  align: 'center',
                  clickEvent: index => { this.showedit(index, '验收入库') },
                  isColComp: true,
                  rowFileds: 'show:orderStatus-11',
                  component: () => import('@/components/org/linkButton')
                },
                {
                  compKey: 'propKey2',
                  labelName: '质量状态编辑',
                  align: 'center',
                  clickEvent: index => { this.showedit(index, '质量状态编辑') },
                  isColComp: true,
                  rowFileds: 'show:orderStatus-12|13',
                  component: () => import('@/components/org/linkButton')
                }
              ]
            },
            {
              prop: 'saleOrderCode',
              label: '订单编号',
              width: 140
            },
            {
              prop: 'salePlanCode',
              label: '销售计划号',
              width: 160
            },
            {
              prop: 'vin',
              label: 'VIN码',
              width: 160
            },
            {
              prop: 'reserveDate',
              label: '下单时间',
              width: 160
            },
            {
              prop: 'saleOrderSign',
              label: '订单标签'
            },
            {
              prop: 'carSeriesCode',
              label: '车系编码'
            },
            {
              prop: 'carSeriesCn',
              label: '车系名称'
            },
            {
              prop: 'carConfigCode',
              label: '基础车型编码',
              width: 160
            },
            {
              prop: 'carConfigCn',
              label: '基础车型名称'
            },
            {
              prop: 'optionalPagName',
              label: '选装包'
            },
            {
              prop: 'carColorName',
              label: '车身颜色'
            },
            {
              prop: 'carIncolorName',
              label: '内饰颜色'
            }
          ],
      tableComponents:
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path] &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents &&
        this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
          .tableComponents.length > 0
          ? this.$ConfigCache.CacheConfig.cacheData[this.$route.path]
            .tableComponents
          : [
            // 订单编号
            {
              compKey: 'compKey1',
              labelName: '订单编号：',
              codeField: 'saleOrderCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            // 销售计划号
            {
              compKey: 'compKey2',
              labelName: '销售计划号：',
              codeField: 'salePlanCode',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            // VIN码
            {
              compKey: 'compKey3',
              labelName: 'VIN码：',
              codeField: 'vin',
              component: () => import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            {
              compKey: 'compKey4',
              labelName: '订单标签：',
              codeField: 'saleOrderSign',
              component: () =>
                import('@/components/org/commonInput'),
              type: 'inputText',
              isMust: true
            },
            // 下单时间
            {
              compKey: 'compKey5',
              labelName: '下单时间：',
              codeField: 'createdStartTime,createdEndTime',
              component: () =>
                import('@/components/org/datePicker/andtDatePicker'),
              type: 'YYYY-MM-DD',
              format: 'yyyy-MM-dd',
              isMust: false
            },
            {
              compKey: 'compKey7',
              labelName: '车系名称：',
              codeField: 'carSeriesId',
              oFields: 'carSeriesId,carSeriesCn',
              customObj: {
                api: lov.listOfCarSeries
              },
              component: () => import('@/components/org/commonSelect'),
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey8',
              labelName: '基础车型名称：',
              codeField: 'carConfigId',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'carConfigId,carConfigCn',
              parentFileds: 'value:carSeriesId',
              customObj: {
                listQuery: { carSeriesId: '' },
                api: lov.queryCarType
              },
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey9',
              labelName: '选装包：',
              codeField: 'optionalPagId',
              component: () => import('@/components/org/commonSelect'),
              parentFileds: 'value:carConfigId',
              oFields: 'optionalPagId,optionalPagName',
              customObj: {
                api: lov.queryCarOptionalPagLinkMessage,
                listQuery: {
                  carConfigId: ''
                }
              },
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey10',
              labelName: '车身颜色：',
              codeField: 'carColorId',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'carColorId,carColorName',
              parentFileds: 'value:carConfigId|carSeriesId|optionalPagId',
              customObj: {
                listQuery: {
                  carConfigId: '', carSeriesId: '', optionalPagId: ''
                },
                api: lov.queryCarColorLinkMessage
              },
              type: 'dropdownList',
              isMul: false,
              isMust: false
            },
            {
              compKey: 'compKey11',
              labelName: '内饰颜色：',
              codeField: 'carIncolorId',
              component: () => import('@/components/org/commonSelect'),
              oFields: 'carIncolorId,carIncolorName',
              parentFileds: 'value:carConfigId|carSeriesId|optionalPagId',
              customObj: {
                listQuery: {
                  carConfigId: '', carSeriesId: '', optionalPagId: ''
                },
                api: lov.queryCarInColorLinkMessage
              },
              type: 'dropdownList',
              isMul: false,
              isMust: false
            }
          ],
      tableButtons: [
        // {
        //   conmpKey: 'btenKey0',
        //   type: '',
        //   size: 'small',
        //   text: '验收入库',
        //   clickEvent: () => this.send(),
        //   position: 'left'
        // },
        {
          conmpKey: 'btenKey2',
          type: 'primary',
          size: 'small',
          clickEvent: () => this.queryTable(1),
          text: '查询',
          position: 'right'
        },
        {
          conmpKey: 'btenKey3',
          type: '',
          size: 'small',
          clickEvent: () => this.reset(),
          text: '重置',
          position: 'right'
        }
      ],
      // 表单查询数据
      formField: this.$utils.getFormField(this, {
        saleOrderCode: '',
        salePlanCode: '',
        vin: '',
        saleOrderSign: '',
        createdDate: '',
        carConfigId: '',
        carSeriesId: '',
        optionalPagId: '',
        carColorId: '',
        carIncolorId: '',
        createdStartTime: '',
        createdEndTime: ''
      }),
      selectionList: [],
      sList: [],
      callbackObj: {
        // 当选择项发生变化时触发该函数
        onCheckSelectChange: (selection) => {
        }
      }
    }
  },
  mounted() {
    setTimeout(() => {
      this.queryTable(1)
    }, 0)
  },
  methods: {
    // 编辑&&验收入库
    showedit(index, type) {
      this.titleName = type
      this.edit(index)
    },
    send(saveObj) {
      this.selectionList = saveObj
      this.sList = saveObj.reduce((pre, cur) => {
        pre.push({
          SALE_ORDER_SIGN: cur.SALE_ORDER_SIGN,
          UPDATE_CONTROL_ID: cur.UPDATE_CONTROL_ID,
          carConfigCode: cur.carConfigCode,
          carColorCode: cur.carColorCode,
          carIncolorCode: cur.carIncolorCode,
          optionalPagCode: cur.optionalPagCode,
          custType: cur.custType,
          reserveDate: cur.reserveDate,
          salePlanCode: cur.salePlanCode,
          assignDate: cur.assignDate,
          hostQualityState: cur.hostQualityState,
          hostQualityPic: cur.hostQualityPic,
          remark: cur.remark,
          saleOrderCodeC: cur.saleOrderCodeC,
          deliveryFacHwCode: cur.deliveryFacHwCode
        })
        return pre
      }, [])
      const obj = {
        vin: [],
        saleOrderId: [],
        updateControlIdS: [],
        updateControlIdO: [],
        saleOrderDate: [],
        custType: [],
        carConfigCode: [],
        carColorCode: [],
        carIncolorCode: [],
        optionalPagCode: [],
        assignDate: [],
        saleOrderCodeC: [],
        updateControlIdD: []
      }
      // 发送请求
      for (const k in this.selectionList) {
        obj.vin.push(this.selectionList[k].vin)
        obj.saleOrderId.push(this.selectionList[k].saleOrderId)
        obj.updateControlIdS.push(this.selectionList[k].updateControlIdS)
        obj.updateControlIdO.push(this.selectionList[k].updateControlIdO)
        obj.saleOrderDate.push(this.selectionList[k].saleOrderDate)
        obj.custType.push(this.selectionList[k].custType)
        obj.carConfigCode.push(this.selectionList[k].carConfigCode)
        obj.carColorCode.push(this.selectionList[k].carColorCode)
        obj.carIncolorCode.push(this.selectionList[k].carIncolorCode)
        obj.optionalPagCode.push(this.selectionList[k].optionalPagCode)
        obj.assignDate.push(this.selectionList[k].assignDate)
        obj.saleOrderCodeC.push(this.selectionList[k].saleOrderCodeC)
        obj.updateControlIdD.push(this.selectionList[k].updateControlIdD)
      }
      this.$requestAPI(this.$getParams({
        apiConfig: api.carCheckPutStorage,
        variables: { ...obj, infoList: this.sList }
      })).then(response => {
        if (response.msg) {
          this.$message({
            message: response.msg,
            type: 'warn',
            uration: 2000
          })
        }
        if (response.result === '1') {
          this.close('1')
        }
      }).catch(err => {
        console.info(err)
      })
    }
  }
}
</script>

<style></style>
